package freecomm.study.alarmtest;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    private Button setButton;
    private Button cancelButton;
    private final static String ALARM_ACTION = "freecomm.study.alarmtest.intent.action.ALARM_ACTION";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setButton = (Button) findViewById(R.id.setBtn);
        cancelButton = (Button) findViewById(R.id.cancelBtn);
        final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent = new Intent();
        intent.setAction(ALARM_ACTION);
        intent.putExtra("msg", "shit!!!!");
        final PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this,
                0, intent, 0);
        final long time = System.currentTimeMillis();
        setButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                am.setRepeating(AlarmManager.RTC_WAKEUP, time, 8 * 1000, pi);
            }
        });
        cancelButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                am.cancel(pi);
            }
        });
    }
}